package com.itboye.ihomebank.key;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.itboye.ihomebank.KeyFragment;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.ttKey.BleConstant;
import com.itboye.ihomebank.ttKey.FoundDeviceAdapter;
import com.itboye.ihomebank.ttKey.Operation;
import com.ttlock.bl.sdk.scanner.ExtendedBluetoothDevice;

/**
 * 附近设备
 * 
 */
public class ActivitySheBei extends BaseOtherActivity {
	ImageView close_icon;
	TextView add_shap_title_tv;

	XListView shebei_listview;

	public ProgressDialog progressDialog;

	private List<ExtendedBluetoothDevice> devices;
	private FoundDeviceAdapter foundDeviceAdapter;
	private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			final String action = intent.getAction();
			if (action.equals(BleConstant.ACTION_BLE_DEVICE)) {
				Bundle bundle = intent.getExtras();
				ExtendedBluetoothDevice device = bundle
						.getParcelable(BleConstant.DEVICE);
				foundDeviceAdapter.updateDevice(device);
			} else if (action.equals(BleConstant.ACTION_BLE_DISCONNECTED)) {
				cancelProgress();
				Toast.makeText(ActivitySheBei.this, "蓝牙已断开,请重新添加.",
						Toast.LENGTH_LONG).show();
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		return R.layout.activity_she_bei;
	}

	@Override
	protected void onInitController() {

		devices = new ArrayList<ExtendedBluetoothDevice>();
		foundDeviceAdapter = new FoundDeviceAdapter(this, devices);
		shebei_listview.setAdapter(foundDeviceAdapter);
		registerReceiver(mReceiver, getIntentFilter());

		shebei_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				KeyFragment.bleSession.setOperation(Operation.ADD_ADMIN);
				KeyFragment.mTTLockAPI.connect((ExtendedBluetoothDevice) foundDeviceAdapter.getItem(position-1));
				showProgress();
			}
		});

	}

	private IntentFilter getIntentFilter() {
		final IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(BleConstant.ACTION_BLE_DEVICE);
		intentFilter.addAction(BleConstant.ACTION_BLE_DISCONNECTED);
		return intentFilter;
	}

	@Override
	protected void onInit() {
		add_shap_title_tv.setText("附近设备");
		
		registerReceiver(mReceiver, getIntentFilter());

	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

	private void showProgress() {
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setMessage("请等待……");
		}
		progressDialog.show();
	}

	public void cancelProgress() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mReceiver);
	}
}
